import time
import URL as up
import requests
import base64
import os
from PIL import Image, ImageDraw, ImageFont
import pandas as pd

# post请求返回json
def Revd(file):
    with open(f"./image//{file}", 'rb') as f:
        image = base64.b64encode(f.read()).decode()
    data = {'image': image}
    client = requests.post(up.REVD, params=up.Params, headers=up.Headers, data=data)
    return client.json()

# image写入文字
def Read(file, text):
    fileimage = Image.open(f"image//{file}")
    draw = ImageDraw.Draw(fileimage)
    font = ImageFont.truetype(r'C:\Windows\Fonts\msyh.ttc', 30)
    draw.text((0, 0), text=text, fill='red', font=font)
    fileimage.save(f'image//{file}')

# 写入excel
def with_excel():
    fileDir = os.listdir("./image")
    s = []
    for i in fileDir:
        n = ''
        json = Revd(i)
        if json['conclusion'] != '合规':
            n=json['data'][0]['msg']
        date = {
            '文件名': i,
            '是否合规': json['conclusion'],
            '不合规类型': n
        }
        s.append(date)
        time.sleep(0.5)
    df = pd.DataFrame(s)
    df.to_excel('result.xlsx', index=False)
    print("写入完成")
with_excel()
